﻿/****************************************************************************
*                                                                           *
* Copyright (c) Ionescu M. Marius. All rights reserved(2010).               *
*                                                                           *
****************************************************************************/

using System;
using System.Data;
using System.Web.UI.WebControls;

public partial class Catalogs_Users : SessionPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CustomControlsNS.CustomButton cc = new CustomControlsNS.CustomButton(Resources.DatabaseStrings.btn_Save, Resources.DatabaseStrings.btn_Save, "$get('" + btnSave.ClientID + "').click();", "1", true);
        cc.ID = "btnSave";
        this.Master.PageToolbarButtons.Add(cc);

        btnClose.OnClick = "$get('" + CancelButton1.ClientID + "').click();";
        btnClosePublish.OnClick = "$get('" + CancelButton5.ClientID + "').click();";

        if (!IsPostBack)
        {
            ClearSession("ds_Users");
            ClearSession("ds_USGroups");
            ClearSession("ds_US_Selected");
        }
        Load_Data(false);
    }

    private void Load_Data(bool bForce)
    {
        DataSet ds_Users = GetSessionDataSet("ds_Users");
        if (ds_Users == null || bForce)
        {
            ds_Users = Hipocrate.DB.DBConnection.GetData("Users_Select", new string[] { }, new object[] { });

            foreach (DataTable dt in ds_Users.Tables)
                foreach (DataColumn dc in dt.Columns)
                {
                    dc.ReadOnly = false;
                    dc.AllowDBNull = true;
                }

            SetSession("ds_Users", ds_Users);
        }
        if (ds_Users != null && (!IsPostBack || bForce))
        {
            DataView dv0 = ds_Users.Tables[0].DefaultView;

            grStations.SessionKey = GetSessionKey("ds_Users");
            grStations.SessionKeyIndex = 0;
            grStations.SessionKeyFilter = dv0.RowFilter;

            grStations.DataSource = dv0;
            grStations.DataBind();
            grStations.CaptionForXLS = Resources.DatabaseStrings.Users;
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            DataSet ds_Users = GetSessionDataSet("ds_Users");
            if (ds_Users != null)
            {
                Hipocrate.DB.DBConnection.SaveAll(ds_Users,
                    new object[] { 
						new object[] { "Users_Insert", new string[] { "Name", "Passwd", "Email" } }
					},
                    new object[] { 
						new object[] { "Users_Update", new string[] { "ID", "Name", "Passwd", "Email" } }
					},
                    new object[] { 
						new object[] { "Users_Delete", new string[] { "ID" } }
					}
                    );
                this.Master.SetMessage(Resources.DatabaseStrings.DocumentSaved2, false);
                Load_Data(true);
            }
        }
        catch (Exception ex)
        {
            this.Master.SetMessage(ex.Message, true);
        }
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        dvUsersDetail.ChangeMode(DetailsViewMode.Edit);

        ImageButton btnDetails = sender as ImageButton;
        GridViewRow row = (GridViewRow)btnDetails.NamingContainer;
        DataView dv = new DataView((GetSessionDataSet("ds_Users")).Tables[0]);
        dv.RowFilter = "ID = " + Convert.ToString(this.grStations.DataKeys[row.RowIndex].Value);

        this.dvUsersDetail.DataSource = dv;
        this.dvUsersDetail.DataBind();

        btnAdd.Visible = false;
        btnSave2.Visible = true;
        btnDelete.Visible = true;
        up2.Update();
        mdlPopup.Show();
        lblUser.Text = null;
    }

    protected void btnInsert_Click(object sender, EventArgs e)
    {
        dvUsersDetail.ChangeMode(DetailsViewMode.Insert);

        DataView dv = new DataView((GetSessionDataSet("ds_Users")).Tables[0]);
        this.dvUsersDetail.DataSource = dv;
        this.dvUsersDetail.DataBind();

        dvUsersDetail.Rows[0].Visible = false;

        btnAdd.Visible = true;
        btnSave2.Visible = false;
        btnDelete.Visible = false;
        up2.Update();
        mdlPopup.Show();
        lblUser.Text = null;
    }

    protected void btnSave2_Click(object sender, EventArgs e)
    {
        if (((TextBox)dvUsersDetail.Rows[1].Cells[1].Controls[0]).Text == "")
        {
            lblUser.Text = Resources.DatabaseStrings.not_name;
            return;
        }
        if (((TextBox)dvUsersDetail.Rows[2].Cells[1].Controls[0]).Text == "")
        {
            lblUser.Text = Resources.DatabaseStrings.not_pass;
            return;
        }
        if (((TextBox)dvUsersDetail.Rows[3].Cells[1].Controls[0]).Text == "")
        {
            lblUser.Text = Resources.DatabaseStrings.not_email;
            return;
        }
        if (((TextBox)dvUsersDetail.Rows[1].Cells[1].Controls[0]).Text.Length > 100)
        {
            lblUser.Text = Resources.DatabaseStrings.length_name;
            return;
        }

        string id = dvUsersDetail.DataKey.Value.ToString();
        DataView dv = new DataView((GetSessionDataSet("ds_Users")).Tables[0]);
        dv.RowFilter = "ID=" + id;


        dv[0]["Name"] = Tools.GetSQLValue(((TextBox)dvUsersDetail.Rows[1].Cells[1].Controls[0]).Text);
        dv[0]["Passwd"] = Tools.GetSQLValue(((TextBox)dvUsersDetail.Rows[2].Cells[1].Controls[0]).Text);
        dv[0]["Email"] = Tools.GetSQLValue(((TextBox)dvUsersDetail.Rows[3].Cells[1].Controls[0]).Text);

        grStations.DataBind();
        mdlPopup.Hide();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (((TextBox)dvUsersDetail.Rows[1].Cells[1].Controls[0]).Text == "")
        {
            lblUser.Text = Resources.DatabaseStrings.not_name;
            return;
        }
        if (((TextBox)dvUsersDetail.Rows[2].Cells[1].Controls[0]).Text == "")
        {
            lblUser.Text = Resources.DatabaseStrings.not_pass;
            return;
        }
        if (((TextBox)dvUsersDetail.Rows[3].Cells[1].Controls[0]).Text == "")
        {
            lblUser.Text = Resources.DatabaseStrings.not_email;
            return;
        }

        if (((TextBox)dvUsersDetail.Rows[1].Cells[1].Controls[0]).Text.Length > 100)
        {
            lblUser.Text = Resources.DatabaseStrings.length_name;
            return;
        }

        DataView dv = new DataView((GetSessionDataSet("ds_Users")).Tables[0]);
        DataRowView drv = dv.AddNew();


        drv["Name"] = Tools.GetSQLValue(((TextBox)dvUsersDetail.Rows[1].Cells[1].Controls[0]).Text);
        drv["Passwd"] = Tools.GetSQLValue(((TextBox)dvUsersDetail.Rows[2].Cells[1].Controls[0]).Text);
        drv["Email"] = Tools.GetSQLValue(((TextBox)dvUsersDetail.Rows[3].Cells[1].Controls[0]).Text);


        drv.EndEdit();

        grStations.DataBind();
        mdlPopup.Hide();
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        string id = dvUsersDetail.DataKey.Value.ToString();
        DataView dv = new DataView((GetSessionDataSet("ds_Users")).Tables[0]);
        dv.RowFilter = "ID=" + id;
        dv[0].Row.Delete();

        grStations.DataBind();
        mdlPopup.Hide();
    }

    protected bool ShowAssoc(string strID)
    {
        DataSet ds_Users = GetSessionDataSet("ds_Users");
        DataView dv0 = new DataView(ds_Users.Tables[0]);
        dv0.RowFilter = "ID = " + strID;
        return (dv0[0].Row.RowState != DataRowState.Added);
    }

    protected void btnEditFS_to_FSGroups_Click(object sender, EventArgs e)
    {
        ImageButton btnDetails = sender as ImageButton;
        GridViewRow row = (GridViewRow)btnDetails.NamingContainer;
        string strUS_ID = Convert.ToString(this.grStations.DataKeys[row.RowIndex].Value);


        DataSet ds_USGroups = GetSessionDataSet("ds_USGroups");
        if (ds_USGroups == null)
        {
            ds_USGroups = Hipocrate.DB.DBConnection.GetData("[UsersGroups_Select]", new string[] { }, new object[] { });

            foreach (DataTable dt in ds_USGroups.Tables)
                foreach (DataColumn dc in dt.Columns)
                {
                    dc.ReadOnly = false;
                    dc.AllowDBNull = true;
                }

            DataColumn dc2 = new DataColumn("Selected", typeof(bool));
            ds_USGroups.Tables[0].Columns.Add(dc2);

            SetSession("ds_USGroups", ds_USGroups);
        }

        DataView dv = ds_USGroups.Tables[0].DefaultView;

        GridViewUS.SessionKey = GetSessionKey("ds_USGroups");
        GridViewUS.SessionKeyIndex = 0;
        GridViewUS.SessionKeyFilter = dv.RowFilter;

        GridViewUS.DataSource = dv;
        GridViewUS.DataBind();

        btnSelectNone_Click(null, null);

        ClearSession("ds_US_Selected");
        DataSet ds_US_Selected = GetSessionDataSet("ds_US_Selected");
        if (ds_US_Selected == null)
        {
            ds_US_Selected = Hipocrate.DB.DBConnection.GetData("Users_to_UsGroups_Select3", new string[] { "User_Group_ID" }, new object[] { strUS_ID });

            foreach (DataTable dt in ds_US_Selected.Tables)
                foreach (DataColumn dc in dt.Columns)
                {
                    dc.ReadOnly = false;
                    dc.AllowDBNull = true;
                }

            DataColumn dc2 = new DataColumn("Selected", typeof(bool));
            ds_US_Selected.Tables[0].Columns.Add(dc2);

            ds_US_Selected.Tables[0].Columns["ID"].DefaultValue = strUS_ID;

            SetSession("ds_US_Selected", ds_US_Selected);
        }


        DataView dv2 = ds_US_Selected.Tables[0].DefaultView;

        GridViewSelectedUsers.SessionKey = GetSessionKey("ds_US_Selected");
        GridViewSelectedUsers.SessionKeyIndex = 0;
        GridViewSelectedUsers.SessionKeyFilter = dv2.RowFilter;

        GridViewSelectedUsers.DataSource = dv2;
        GridViewSelectedUsers.DataBind();

        up5_3.Update();
        mdlPopupPublish.Show();
    }
    protected void btnSavePublish_Click(object sender, EventArgs e)
    {
        try
        {
            DataSet ds_US_Selected = GetSessionDataSet("ds_US_Selected");
            if (ds_US_Selected != null)
            {
                Hipocrate.DB.DBConnection.SaveAll(ds_US_Selected,
                    new object[] { 
						new object[] { "Users_to_UsGroups_Insert", new string[] { "ID", "User_Group_ID" } }
					},
                    new object[] { 
						new object[] { "Users_to_UsGroups_Update", new string[] { "ID", "User_Group_ID" } }
					},
                    new object[] { 
						new object[] { "Users_to_UsGroups_Delete", new string[] { "ID", "User_Group_ID" } }
					}
                    );
                this.Master.SetMessage(Resources.DatabaseStrings.DocumentSaved2, false);
            }
        }
        catch (Exception ex)
        {
            this.Master.SetMessage(ex.Message, true);
        }
        mdlPopupPublish.Hide();
    }

    protected void btnSelectAll_Click(object sender, EventArgs e)
    {
        DataSet ds_USGroups = GetSessionDataSet("ds_USGroups");
        DataView dv1 = ds_USGroups.Tables[0].DefaultView;
        foreach (DataRowView drv in dv1)
            drv["Selected"] = true;

        GridViewUS.DataSource = dv1;
        GridViewUS.DataBind();
    }

    protected void btnSelectNone_Click(object sender, EventArgs e)
    {
        DataSet ds_USGroups = GetSessionDataSet("ds_USGroups");
        DataView dv1 = ds_USGroups.Tables[0].DefaultView;
        foreach (DataRowView drv in dv1)
            drv["Selected"] = false;

        GridViewUS.DataSource = dv1;
        GridViewUS.DataBind();
    }

    protected void btnSelect_Click(object sender, EventArgs e)
    {
        DataSet ds_USGroups = GetSessionDataSet("ds_USGroups");
        DataSet ds_US_Selected = GetSessionDataSet("ds_US_Selected");
        DataView dv1 = new DataView(ds_USGroups.Tables[0]);
        DataView dv4 = ds_US_Selected.Tables[0].DefaultView;
        dv1.RowFilter = (dv4.RowFilter != "" ? "(" + dv4.RowFilter + ") AND " : "") + "(Selected = 1)";

        foreach (DataRowView drvSrc in dv1)
        {
            DataView dv = new DataView(dv4.Table);
            dv.RowFilter = "User_Group_ID=" + drvSrc["ID"].ToString();
            if (dv.Count == 0)
            {
                DataRowView drvDest = dv4.AddNew();
                drvDest["Selected"] = true;
                drvDest["User_Group_ID"] = drvSrc["ID"].ToString();
                drvDest["Name"] = drvSrc["Name"].ToString();
                drvDest.EndEdit();
            }
        }

        GridViewSelectedUsers.DataSource = dv4;
        GridViewSelectedUsers.DataBind();
        up5_3.Update();
    }

    protected void btnSelectAll2_Click(object sender, EventArgs e)
    {
        DataSet ds_US_Selected = GetSessionDataSet("ds_US_Selected");
        DataView dv = ds_US_Selected.Tables[0].DefaultView;
        foreach (DataRowView drv in dv)
            drv["Selected"] = true;

        GridViewSelectedUsers.DataSource = dv;
        GridViewSelectedUsers.DataBind();
    }

    protected void btnSelectNone2_Click(object sender, EventArgs e)
    {
        DataSet ds_US_Selected = GetSessionDataSet("ds_US_Selected");
        DataView dv = ds_US_Selected.Tables[0].DefaultView;
        foreach (DataRowView drv in dv)
            drv["Selected"] = false;

        GridViewSelectedUsers.DataSource = dv;
        GridViewSelectedUsers.DataBind();
    }

    protected void btnRemoveSelected_Click(object sender, EventArgs e)
    {
        DataSet ds_US_Selected = GetSessionDataSet("ds_US_Selected");
        DataView dv4 = ds_US_Selected.Tables[0].DefaultView;
        DataView dv = new DataView(ds_US_Selected.Tables[0]);
        dv.RowFilter = "Selected = 1";

        for (int i = dv.Count - 1; i >= 0; --i)
            dv[i].Row.Delete();

        GridViewSelectedUsers.DataSource = dv4;
        GridViewSelectedUsers.DataBind();
    }

}
